<?php
class FeedbackAdminForm extends Form{
	function FeedbackAdminForm()
	{
		Form::Form('FeedbackAdminForm');
	}
	
	function draw()
	{
		global $display;
			
		$status_arr=array(
							'0'=>'Chưa trả lời / phản hồi',
							'1'=>'Đã phản hồi / phản hồi',
							'2'=>'Tất cả',
							);
							
		$status	=	(int)Url::get('status',0);
		
		$display->add('url_admin',Url::build_current());
		$display->add('status_option',AZLib::getOption($status_arr,$status));
		
		$item_per_page_arr=array(
								'20'=>'20',
								'50'=>'50',
								'100'=>'100',
								'150'=>'150',
								'200'=>'200',
								'300'=>'300',
								'500'=>'500',
								);
								
		$item_per_page	=	(int)Url::get('row_per_page',20);				
		
		$display->add('row_per_page_option',AZLib::getOption($item_per_page_arr,$item_per_page));
		
		$from_date_arr[-1]='--Từ ngày--';
		for ($i=30;$i>=0;$i--)
		{
			$time_from	=	mktime(0,0,0,date('m',TIME_NOW),date('d',TIME_NOW),date('Y',TIME_NOW))-($i*24*3600);
			$from_date_arr[$time_from]=date('d/m/Y',$time_from);
		}
		$to_date_arr[-1]='--Đến ngày--';
		for ($i=0;$i<=30;$i++)
		{
			$time_to=mktime(23,59,59,date('m',TIME_NOW),date('d',TIME_NOW),date('Y',TIME_NOW))-($i*24*3600);
			$to_date_arr[$time_to]=date('d/m/Y',$time_to);
		}
		$from_date	= Url::get('from_date',-1);
		$to_date	= Url::get('to_date',-1);
		
		$display->add('from_date_option',	AZLib::getOption($from_date_arr,$from_date));
		$display->add('to_date_option',	AZLib::getOption($to_date_arr,$to_date));
					
		
		$sql	=	' 1 ';
		
		if($from_date>-1)
			$sql.= " AND time>=".$from_date;
		if($to_date>-1)
			$sql.= " AND time<=".$to_date;
		
		$txt_ykien	=	Url::get('txt_ykien','');
		
		if($txt_ykien!='')
		{
			$sql.= " AND content like '%$txt_ykien%'";
				
		}
		$display->add('txt_ykien',$txt_ykien);
		
		if ($status!=2)
			$sql.= ' AND  status='.$status;
		
		$num_of_row		=	DB::count('feedback',$sql);
		
		$limit			=	'';
		
		$display->add('pagging',AZPagging::paging_list($limit,$num_of_row,$item_per_page,10,'page_no','',true,'Liên hệ'));
		
		$page_no		=	(int)Url::get('page_no',1);
		
		if($page_no <= 0)
		$page_no = 1;
		
		$i				=	0;
		$all_feedbacks 	= 	array();
		
		$sql 			= 	"SELECT * FROM feedback WHERE $sql ORDER BY id DESC $limit";
		$re				=	DB::query($sql);
		
		if ($re)
		{
			while ($row = mysql_fetch_assoc($re))
			{
				$email = '';
				
				if($row['name'])
				$email	=	stripslashes($row['name'])." ";
				
				if($row['address'])
				$email.="( ".stripslashes($row['address']).") ";
				
				$email.='<a href="mailto:'.$row['email'].'"><font color=blue>'.$row['email']."</font></a>";
				
				$row['email'] = $email;
				
				$row['date'] = date("d/m/Y H:i:s",$row['time']);
				$row['date2'] = AZLib::duration_time($row['time']);
				
				$row['url_del'] 	= AZLib::buttonDel(Url::build_current(array('id'=>$row['id'],'cmd'=>'delete')),'style/images/delete.gif','Xóa ý kiến');
				
				$row['url_reply'] 	= Url::build_current(array('id'=>$row['id'],'cmd'=>'reply'));
				
				if($row['reply_content'])
					$row['reply_content'] = stripslashes($row['reply_content']);
				else
					$row['reply_content'] = "Chưa có";
					
				if(!$row['status'])
					$row['exam'] 	= AZLib::button(Url::build_current(array('id'=>$row['id'],'cmd'=>'exam')),'style/images/exam.gif','Duyệt liên hệ');
				else	
					$row['exam'] 	= AZLib::button(Url::build_current(array('id'=>$row['id'],'cmd'=>'unexam')),'style/images/unexam.gif','Bỏ duyệt liên hệ');
				
				if ($i%2==0)
				{
					$row['bgcolor'] = '#FFFFFF';
				}
				else
				{
					$row['bgcolor'] = '#CCFF99';
				}
				
				$i++;
				
				$row['i'] = ($page_no-1)*$item_per_page +$i;
				
				$all_feedbacks[$row['id']] = $row;
			}
		}
		
		$display->add('all_feedbacks',$all_feedbacks);
		
		$this->beginForm();
		$display->output("FeedbackAdmin");
		$this->endForm();
	}
}
?>